import wepy from 'wepy';
import wxutils from './WxUtils';
import pagemixin from '../mixins/pagemixin';
import Tips from './Tips';

export default class basepage extends wepy.page {
    data = {
    };

    config = {}

    mixins = this.minbase(); // 声明页面所引用的Mixin实例
    ///动态获取mixins配置信息
    minbase() {
        var tempmixins = [];
        if (this.addmixins) {

            tempmixins = this.addmixins();
            if (tempmixins == null || !Array.isArray(tempmixins)) {
                tempmixins = [];
            }
            tempmixins.push(pagemixin);
        }
        else {
            tempmixins.push(pagemixin);
        }
        return tempmixins;
    }

    ///基类加载方法，
    async onLoad(option) {
        let that = this;
        wxutils.tryRenderPage(
            function () {
                return that.bindApiData(option).then(res => {

                    that.pageIsReady = true;
                    wx.setNavigationBarTitle({
                        title: that.pageTitle
                    });///设置页面标题
                    that.$apply();

                }).catch(ex => {                    
                    wxutils.logError(ex);
                    that.init = true;
                    that.$apply();
                    if(!ex.hasSendDebug){
                      console.log("页面加载失败", ex);
                    }
                    wxutils.goToErrorPage(500);//

                });
            });
    }

    goToErrorPage(bizcode,msg=null) {
        wxutils.goToErrorPage(bizcode,msg);
    }


    ///新增成功、删除成功、编辑成功
    showSuccess(title) {
        this.closeLoading();
        return Tips.success(title);
    }

    ///显示加载层
    showLoading(title = "") {

        Tips.loading(title);
    }

    ///关闭加载层
    closeLoading() {
        this.btn_closeload();//关闭加载状态
        Tips.loaded();
    }

    ///wxalert提示，是否上报错误信息
    async showAlert(res, title = "提示", sendToFundebug = false, keyName = null) {
        this.closeLoading();

        this.sendMsgToFundebug(res, sendToFundebug, keyName);
        let msg = this.getMsgDetail(res);

        return Tips.alert(msg, title).then(rs => {
            return "close"
        });
    }

    async showConfirm(text, title = '确认提示') {
         
        this.closeLoading();
        return Tips.confirm(text, title).then(res => {
            return res.confirm;
        });
    }

    ///资源不存在的提示，如会员被删除了、商品不存在了
    async show404Msg(res, msg="请求错误，请稍后再试",sendToFundebug = false, keyName = null) {

        ///404页面级
        if(!this.pageIsReady && res.ApiPath)
        {
            this.sendMsgToFundebug(res, sendToFundebug, keyName);///判断是否上报

            console.error(res.ApiPath+"接口返回异常",res);
            wxutils.goToErrorPage(404,msg);///系统开小
            return "stop";
        }

        this.closeLoading();
        this.sendMsgToFundebug(res, sendToFundebug, keyName);///判断是否上报
        
        let tipMsg="请求错误，请稍后再试";///404操作级返回
        return Tips.text(tipMsg).then(rs => {
            return "end"
        });

    }

    ///通用错误提示，2秒钟，是否上报错误信息
    async showErrorMsg(res, sendToFundebug = false, keyName = null) {

        ///页面级操作时，系统开小差
        if(!this.pageIsReady)
        {
            this.sendMsgToFundebug(res, sendToFundebug, keyName);
            console.error(res.ApiPath,"异常",res)
            wxutils.goToErrorPage(500);///系统开小
            return "stop";
        }


        this.closeLoading();
        this.sendMsgToFundebug(res, sendToFundebug, keyName);
        let msg = this.getMsgDetail(res);
        return Tips.text(msg).then(rs => {
            return "end"
        });

    }

    ///主动上报日志对象到fundebug
    async sendMsgToFundebug(res, sendToFundebug = true, keyName = null) {
        if (sendToFundebug) {
            
            if (keyName == null) {
                keyName = res.ApiPath;
            }
            if (keyName == null) {
                keyName = wxutils.getCurrentPageUrl();
            }

            wxutils.logNotify(keyName, res);
        }
    }

    ///获取信息详情
    getMsgDetail(res) {
        let msg = "";
        if (typeof (res) == "object") {
            msg = res.Msg;
        }
        else {
            msg = res;
        }
        return msg;
    }

    ///fundebug上报异常
    async logError(ex) {
        wxutils.logError(ex);
    }


    async awtRs(asyncFun) {        
        var res = await wxutils.awtAsyncFun(asyncFun).catch(ex=>{
            this.showErrorMsg("系统开⼩差了，程序员⼩哥正在抢修中", false);
            throw(ex);
        });        
        if (res&&res.Next && res.HttpCode && res.HttpCode != 200) {
           
            res.Next = false;
            if (!this.pageIsReady) {
                
                ///页面级的非200状态统一处理
                this.sendMsgToFundebug(res);
                this.goToErrorPage(res.HttpCode);

            } else {

                ///操作级的非200状态的统一处理
                switch (res.HttpCode) {
                    case 500:

                        this.showErrorMsg("系统开⼩差了，程序员⼩哥正在抢修中", false);
                        this.sendMsgToFundebug(res);///这种状态需要上报信息
                        break;

                    case 400:
                        let pname=res.Pname;
                        if(!panme || panme=="")
                        {
                            pname=res.Msg;
                        }
                        else
                        {
                            pname="参数校验错误："+pname;
                        }

                        this.showErrorMsg(pname, false);
                        this.sendMsgToFundebug(res);///这种状态需要上报信息
                        break;

                    case 502:
                    case 503:
                        this.showErrorMsg("网络不给力，请稍后再试", false);
                        break;

                    case 504:
                        this.showErrorMsg("网络不给力，请稍后再试", false);
                        break;

                    case 404:
                        this.showErrorMsg("请求错误，请稍后再试");
                        this.sendMsgToFundebug(res);///这种状态需要上报信息
                        break;

                    default:
                        this.showErrorMsg(res, true);
                }
            }

        }
        return res;
    }

    ///在这里面执行数据绑定逻辑
    async bindApiData(option) {
        throw new error("未实现基类basepagebindApiData方法");///这个方法需子类实现
    }
}
